{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {
    "collapsed": false,
    "deletable": true,
    "editable": true
   },
   "outputs": [],
   "source": [
    "%matplotlib inline\n",
    "import os\n",
    "import sys\n",
    "import matplotlib.pyplot as plt\n",
    "import numpy as np\n",
    "\n",
    "sCaffeFolder =  '/home/gines/devel/openpose_caffe_train/'\n",
    "# sBasePath = '/media/posefs3b/Users/gines/openpose_train/training_results/1_19Binary/pose/body_19/'\n",
    "# sBasePath = '/media/posefs3b/Users/gines/openpose_train/training_results/1_19Binary2/pose/body_19/'\n",
    "# sBasePath = '/media/posefs3b/Users/gines/openpose_train/training_results/2_19bOcclusion/pose/body_19b/'\n",
    "sBasePath = '/media/posefs3b/Users/gines/openpose_train/training_results/2_19MoreScale2/pose/body_19/'\n",
    "sFirstBinaryLayer = 'Mconv4_stage1_L2'\n",
    "\n",
    "sProtoTxt =  sBasePath + 'pose_deploy.prototxt'\n",
    "# sCaffemodelPath =  sBasePath + 'pose_iter_202000.caffemodel'\n",
    "# sCaffemodelPath =  sBasePath + 'pose_iter_12000.caffemodel'\n",
    "# sCaffemodelPath =  sBasePath + 'pose_iter_336000.caffemodel'\n",
    "sCaffemodelPath =  sBasePath + 'pose_iter_776000.caffemodel'\n",
    "\n",
    "# Caffe includes\n",
    "sys.path.insert(0, os.path.join(sCaffeFolder, 'python'))\n",
    "import caffe\n",
    "from caffe import layers as L  # pseudo module using __getattr__ magic to generate protobuf messages\n",
    "from caffe import params as P  # pseudo module using __getattr__ magic to generate protobuf messages\n",
    "\n",
    "def net_to_py_readable(protoTxtPath, caffemodelPath):\n",
    "    net = caffe.Net(protoTxtPath, caffemodelPath, caffe.TEST) # read the net + weights\n",
    "    pynet_ = []\n",
    "    for li in xrange(len(net.layers)):  # for each layer in the net\n",
    "        layer = {}  # store layer's information\n",
    "        layer['name'] = net._layer_names[li]\n",
    "        # for each input to the layer (aka \"bottom\") store its name and shape\n",
    "        layer['bottoms'] = [(net._blob_names[bi], net.blobs[net._blob_names[bi]].data.shape) \n",
    "                                                 for bi in list(net._bottom_ids(li))] \n",
    "        # for each output of the layer (aka \"top\") store its name and shape\n",
    "        layer['tops'] = [(net._blob_names[bi], net.blobs[net._blob_names[bi]].data.shape) \n",
    "                                            for bi in list(net._top_ids(li))]\n",
    "        layer['type'] = net.layers[li].type  # type of the layer\n",
    "        # the internal parameters of the layer. not all layers has weights.\n",
    "        layer['weights'] = [net.layers[li].blobs[bi].data[...] \n",
    "                                                for bi in xrange(len(net.layers[li].blobs))]\n",
    "        pynet_.append(layer)\n",
    "    return pynet_\n",
    "\n",
    "sNetParameters = net_to_py_readable(sProtoTxt, sCaffemodelPath)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {
    "collapsed": false,
    "deletable": true,
    "editable": true
   },
   "outputs": [],
   "source": [
    "startRecording = False\n",
    "for netParameter in sNetParameters:\n",
    "    if netParameter['name'] == sFirstBinaryLayer:\n",
    "        startRecording = True\n",
    "    if netParameter['type'] == 'Convolution' and startRecording:\n",
    "        # weights = netParameter['weights'][0]\n",
    "        # bias = netParameter['weights'][1]\n",
    "        weightsArray = np.asarray(netParameter['weights'][0], dtype=np.float32)\n",
    "        if weightsArray.shape[2] > 1 and weightsArray.shape[3] > 1:\n",
    "            weightsArray = weightsArray.reshape(np.prod(weightsArray.shape), 1).copy()\n",
    "            plt.hist(weightsArray, normed=True, bins=100)\n",
    "            plt.ylabel('Probability');\n",
    "            plt.xlabel('Weight value');\n",
    "#             plt.ylim(0, ymax)\n",
    "            plt.xlim(-0.15, 0.15)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {
    "collapsed": false,
    "deletable": true,
    "editable": true
   },
   "outputs": [
    {
     "data": {
      "image/png": "iVBORw0KGgoAAAANSUhEUgAAAYoAAAEKCAYAAAAMzhLIAAAABHNCSVQICAgIfAhkiAAAAAlwSFlz\nAAALEgAACxIB0t1+/AAAFIpJREFUeJzt3X+8bXVd5/HX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Y2B5m+2QJsmg0LpVVf8CbEny/XR/2V8JXEUXHgvAdVX17SQvpJtm4oeBpwHPSPK8\nZbv7Kbo5/J9INynhc5a9fm9VPR34c+A3qup2uvuC/FFVPa2q/n6gXg/STWPykwBJngV8saq+DHwS\neHZVHUE3PfTrhm3vkO2Qxs6g0Hp3BV1IbA2KKweef6rf5oX9zzV0PYzD6b5wB/0I8P6qerCq7qab\nW2vQB/vHzXSBspLzgZ/tl0/qn0M3gdslSa4DfpOHDpWtZJh2SGM31dljpQnYepziKXRDT18Cfh34\nOvD2fpsAf1BVf7GGch7oH7/DcP9vrgQen2QD3SSVb+zX/wlwVlVdmOQo4L9u471b6P+I64+9bL3t\n7TjaIa2aPQqtd1fQzTh7X1V9p6ruA/alGzraeiD7EuDVSfYCSHJQHn5f7k8BL+uPLxzIQ6eZ3p77\ngb239UJ1k6h9CDgLuKmqvtK/tA/fnRJ6e/divh14Rr98AvCIVbRDGjuDQuvddXRnO3162bqvVdW9\nAFX1EeDdwJX9kM8FPPwL/gN0s23eSDf78NV0d3Rr+WvgJ5cfzB5wPnAy3x12gq4H8f4km4F7t7Pf\nvwSen+SzdIH3zVW0Qxo7Z4+Vekn2qqpvJNmfblrtI/vjFdJOzWMU0ndd1J/Sujvwe4aE1LFHIUlq\n8hiFJKnJoJAkNRkUkqQmg0KS1GRQSJKaDApJUtP/AyoQ07eLr9XMAAAAAElFTkSuQmCC\n",
      "text/plain": [
       "<matplotlib.figure.Figure at 0x7f5035dd0290>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "startRecording = False\n",
    "for netParameter in sNetParameters:\n",
    "    if netParameter['name'] == 'conv4_3_CPM':\n",
    "        # weights = netParameter['weights'][0]\n",
    "        # bias = netParameter['weights'][1]\n",
    "        weightsArray = np.asarray(netParameter['weights'][0], dtype=np.float32)\n",
    "        if weightsArray.shape[2] > 1 and weightsArray.shape[3] > 1:\n",
    "            weightsArray = weightsArray.reshape(np.prod(weightsArray.shape), 1).copy()\n",
    "            plt.hist(weightsArray, normed=True, bins=101)\n",
    "            plt.ylabel('Probability');\n",
    "            plt.xlabel('Weight value');\n",
    "#             plt.ylim(0, ymax)\n",
    "            plt.xlim(-0.15, 0.15)\n"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 2",
   "language": "python",
   "name": "python2"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 2
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython2",
   "version": "2.7.12"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
